/**
 * 
 */
package ru.athena.runTool.blockTypes;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class StoredObject extends ExecutableBlock {
	private String	objName_;
	
	public StoredObject(String code, String objectName) {
		super(code);
		objName_ = objectName;
	}
	
	@Override
	public void execute(Connection conn) throws Exception {
		super.execute(conn);
		// check for compile errors
		PreparedStatement pstmt = null;
		try {
			String	compileErrorText = new String();
			String  objSpec = new String();
			pstmt = conn.prepareStatement(
					"select type, name, text from user_errors where name = ? order by sequence");
			pstmt.setString(1, objName_.toUpperCase());
	        ResultSet results = pstmt.executeQuery();
	        for ( boolean hasData = results.next(); hasData; hasData = results.next() ) {
	        	compileErrorText += results.getString("text") + "\n";
	        	objSpec = results.getString("type").toLowerCase() + " " + objName_ + ":\n";
	        }
	        if ( !compileErrorText.equals("")) {
	        	throw new SQLException(objSpec + compileErrorText);
	        }
	        
		} catch (SQLException e) {
			if ( pstmt != null ) {	
				pstmt.close();
			}
			throw e;
		}
		
	}	
	
	public String getName() {
		return objName_; 
	}
}